﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace Common
{
    public class ApiHelper
    {
        private static string getStationInfoUrl = "/api/Interaction/GetStationList";

        private static string getStationDataUrl = "/api/Interaction/GetStationData";

        public static async Task<StationInfoResults> GetStaionInfo(string serverip)
        {
            using (HttpClient client = new HttpClient())
            {
                client.Timeout = TimeSpan.FromSeconds(5);
                var content = new StringContent("{}", Encoding.UTF8, "application/json");
                var postResult = await client.PostAsync(serverip + getStationInfoUrl, content);
                postResult.EnsureSuccessStatusCode();
                var temp = await postResult.Content.ReadAsStringAsync();
                var result = JsonConvert.DeserializeObject<StationInfoResults>(temp);
                result.IsSuccess = true;
                return result;
            }
        }


        public static async Task<(StationDataResult,string)> GetStationData(string body, string serverip)
        {
            if (string.IsNullOrEmpty(body))
            {
                throw new Exception("Para is null");
            }
            using (HttpClient client = new HttpClient())
            {
                client.Timeout = TimeSpan.FromSeconds(5);

                var para = new { Body = new { } };
                var content = new StringContent(body, Encoding.UTF8, "application/json");
                var postResult = await client.PostAsync(serverip + getStationDataUrl, content);
                postResult.EnsureSuccessStatusCode();
                var temp = postResult.Content.ReadAsStringAsync().Result;
                var result = JsonConvert.DeserializeObject<StationDataResult>(temp);
                result.IsSuccess = true;
                return (result,temp);

            }
        }
    }

    #region 实体类

    public class OperateResult
    {
        public string ErrInfo { get; set; }

        public bool IsSuccess { get; set; }

        public string Result { get; set; }
    }

    public class OperateResultBase
    {
        public int errCode { get; set; }

        public bool status { get; set; }

        public string errMessage { get; set; }

        public bool IsSuccess { get; set; }

        public string QueryErrMsg { get; set; }
    }

    public class StationInfoResults : OperateResultBase
    {
        public OperateResultDDBase data { get; set; }
    }

    public class OperateResultDDBase
    {
        public List<StationInfo> list { get; set; }
    }

    public class StationInfo
    {
        public long id { get; set; }

        public int code { get; set; }

        public string name { get; set; }
    }


    public class StationDataResult : OperateResultBase
    {
        public List<StationData> data { get; set; }
    }

    public class StationData
    {
        public string parameterConfigName { get; set; }

        public string parameterValue { get; set; }
    }
    #endregion
}
